mod block;
mod document;
mod engine;
pub mod variables;

pub use block::*;
pub use document::*;
pub use engine::*;

use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::{error::Error, modeling::ComparsionOperator};

#[derive(Debug, Serialize, Default, Deserialize, Clone, PartialEq)]
pub enum WorkflowScriptLanguage {
    #[default]
    #[serde(rename = "javascript")]
    JavaScript,
    #[serde(rename = "python3")]
    Python3,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowErrorStrategy {
    #[serde(rename = "fail-branch")]
    FailBranch,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowErrorHandleMode {
    #[serde(rename = "terminated")]
    Terminated,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowHttpMethod {
    #[default]
    #[serde(rename = "get")]
    Get,
    #[serde(rename = "post")]
    Post,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowHttpBodyType {
    #[default]
    #[serde(rename = "none")]
    None,
    #[serde(rename = "form-data")]
    FormData,
    #[serde(rename = "json")]
    Json,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowHttpBodyItemType {
    #[default]
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "file")]
    File,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowHttpBodyItemBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowHttpBodyItem {
    pub id: String,
    pub r#type: WorkflowHttpBodyItemType,
    pub key: String,
    pub value: Option<String>,
    pub file: Option<ValueSelector>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowHttpBodyBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowHttpBody {
    pub r#type: WorkflowHttpBodyType,
    pub data: Vec<WorkflowHttpBodyItem>,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowHttpAuthorizationType {
    #[default]
    #[serde(rename = "no-auth")]
    NoAuth,
}

//
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum DataOperatorMethod {
    #[default]
    #[serde(rename = "find")]
    Find,
    #[serde(rename = "findone")]
    FindOne,
    #[serde(rename = "update")]
    Update,
    #[serde(rename = "insert")]
    Insert,
    #[serde(rename = "delete")]
    Delete,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowHttpAuthorizationBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowHttpAuthorization {
    pub r#type: WorkflowHttpAuthorizationType,
    pub config: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowHttpRequestTimeoutBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowHttpRequestTimeout {
    pub max_connect_timeout: Option<u32>,
    pub max_read_timeout: Option<u32>,
    pub max_write_timeout: Option<u32>,
    pub connect: Option<u32>,
    pub read: Option<u32>,
    pub write: Option<u32>,
}

pub type ValueSelector = (String, String);

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowModelBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowModel {
    pub provider: String,
    pub name: Option<String>,
    pub mode: Option<String>,
    pub completion_params: Option<WorkflowLlmCompletionParams>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowHostBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowHost {}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowLlmCompletionParamsBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowLlmCompletionParams {
    pub temperature: Option<f32>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowLlmContextBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowLlmContext {
    pub enabled: bool,
    pub variable_selector: ValueSelector,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowListFilterBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowListFilter {
    pub enabled: bool,
    pub conditions: Vec<WorkflowListFilterCondition>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowListFilterConditionBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowListFilterCondition {
    pub key: String,
    pub comparsion_operator: ComparsionOperator,
    pub value: String,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowListExtractBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowListExtract {
    pub enabled: bool,
    pub serial: String,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowListOrderBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowListOrder {
    pub enabled: bool,
    pub key: String,
    pub value: String,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowListLimitBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowListLimit {
    pub enabled: bool,
    pub size: Option<u32>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowTTSOptionsBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowTTSOptions {}

//
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum PromptSpeechType {
    #[serde(rename = "template")]
    Template,
    #[default]
    #[serde(rename = "clone")]
    Clone,
    #[serde(rename = "clone_with_var")]
    CloneWithVar,
}
//
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum Gender {
    #[serde(rename = "男")]
    Nan,
    #[default]
    #[serde(rename = "女")]
    NV,
}

//
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "PromptSpeechSelectorBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct PromptSpeechSelector {
    pub prompt_text_selector: ValueSelector,
    pub prompt_speech_selector: ValueSelector,
}

//
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "PromptSpeechTemplateBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct PromptSpeechTemplate {
    pub gender: Gender,
    pub template: String,
}

//
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "PromptSpeechFileBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct PromptSpeechFile {
    pub prompt_text: String,
    pub prompt_speech: String,
}
